﻿using Microsoft.TeamFoundation.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Vision.Utilities.VisualStudioTool
{
    public partial class TFSAuthenticationForm : Form
    {
        public TFSAuthenticationForm()
        {
            InitializeComponent();
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         
        #region Form Load
        private void TFSAuthenticationForm_Load(object sender, EventArgs e)
        {
            TfsCollectionUrlTextBox.Text = Util.DefaultTFSURL;
            UserNameTextBox.Text = Util.DefaultUserName;
        }
        #endregion

        private void ConnectButton_Click(object sender, EventArgs e)
        {
            try
            {
                var credential = new NetworkCredential(UserNameTextBox.Text, PasswordTextBox.Text);

                var tfsTeamProjectCollection = new TfsTeamProjectCollection(new Uri(TfsCollectionUrlTextBox.Text), credential);

                tfsTeamProjectCollection.EnsureAuthenticated();
                if (tfsTeamProjectCollection.HasAuthenticated)
                {
                    Util.AuthenticatedTfsTeamProjectCollection = tfsTeamProjectCollection;
                    Close();
                }
                else
                {
                    MessageBox.Show("Authenticated failed, please correct username and password.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Authenticated failed: " + ex.Message);
            }
        }
    }
}
